package org.pocketmoney.gui.components;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;

import org.pocketmoney.core.entities.record.*;



public class JRecordsList extends JList<Record>
{

	public JRecordsList(ListModel<Record> dataModel)
	{
		super(dataModel);
	}

	private class RecordRender extends DefaultListCellRenderer
	{

		private static final long serialVersionUID = 6006811423359065430L;

		@Override
		public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus)
		{
			if (isSelected) {
	            setForeground(list.getSelectionForeground());
	            setBackground(list.getSelectionBackground());
	        } else {
	            setForeground(list.getForeground());
	            setBackground(list.getBackground());
	        }
			IRecord record = (IRecord) value;
			String txt = record.getType() + " (" + record.getUser() + "): " + record.getDescription();
			txt = (txt.length() > 55) ? txt.substring(0, 50) + "..." : txt;
			setText(txt);
			return this;
		}

		
	}
	
	private static final long serialVersionUID = 1L;
	private final ListCellRenderer<Object> render = new RecordRender();
	
	@Override
	public ListCellRenderer<Object> getCellRenderer()
	{
		return render;
	}

}
